#include "redis.h"
#include <iostream>

using namespace std;
using namespace sw::redis;

void test()
{
    sw::redis::ConnectionOptions option;
    option.password = "hyk";
    option.host = "127.0.0.1";
    sw::redis::Redis redis(option);

    /*test1 get set
    get（sw::redis::StringView key) 返回sw::redis::optionalString, 使用*号获取值*/
    int ret = redis.set("k1", "v1"); // set
    // cout << ret << endl;
    cout << *(redis.get("k1")) << endl;      // get1
    cout << redis.get("k1").value() << endl; // get2

    /*插入迭代器（insert_iterate）的使用*
       keys * 的使用*/
    vector<string> keys;
    auto ins = inserter(keys, keys.begin());
    redis.keys("*name*", ins); // stringView , Output
    int n = 1;
    for (auto it = keys.begin(); it != keys.end(); it++)
    {
        printf("redis > %d) %s\n", n++, it->c_str());
    }

    /**/
    keys.clear();
    ins = std::inserter(keys, keys.begin()); // 初始化插入位置

    /*TTL, expire, pttl 可以使用毫秒级别过期时间，更高的时间精度*/
    bool b = redis.set("mykey", "123");
    printf("redis > %s\n", b ? "ok" : "(nil)");
    b = redis.expire("mykey", chrono::seconds(20));
    printf("expire mykey 10 secs\n");
    printf("redis > %d\n", b ? 1 : 0);

    sleep(2);
    long long ttl = redis.ttl("mykey");
    printf("redis > %lld\n", ttl);

    /*type*/
    long long c = redis.lpush("mylist", {"value", "value2"});
    printf("redis > %lld\n", c);
    string type = redis.type("mylist");
    printf("redis > %s\n", type.c_str());

    /*mset*/
    vector<pair<string, string>> ksvs = {
        {"k1", "v1"},
        {"k2", "v2"}};
    redis.mset(ksvs.begin(), ksvs.end());
    sw::redis::OptionalString str = redis.get("k2");
    cout << *str << endl;


    /*lpush的三种方法， 单值，初始化列表， 迭代器*/
    redis.lpush("list1", "value1");
    redis.lpush("list", {"value1", "value2", "value3"});
    vector<string> data = {"valuse", "fsdf", "efdsf"};
    redis.lpush("list2", data.begin(), data.end());

    redis.lrange("list", 0, -1, ins);
    for (auto i = keys.begin(); i != keys.end(); i++)
    {
        cout << "list : " << *i << endl;
    }
    keys.clear();
    ins = std::inserter(keys, keys.begin()); // 初始化插入位置

    redis.lrange("list1", 0, -1, ins);
    for (auto i = keys.begin(); i != keys.end(); i++)
    {
        cout << "list1 : " << *i << endl;
    }
    keys.clear();
    ins = std::inserter(keys, keys.begin()); // 初始化插入位置

    redis.lrange("list2", 0 , -1, ins);
    for (auto i = keys.begin(); i != keys.end(); i++)
    {
        cout << "list2 : " << *i << endl;
    }
    keys.clear();
    ins = std::inserter(keys, keys.begin()); // 初始化插入位置

}